package cn.edu.njupt.post.dao;

import cn.edu.njupt.post.model.Feed;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface FeedDAO {
    String TABLE_NAME = " feed ";
    String INSERT_FIELDS = " user_id, data, created_date, type ";
    String SELECT_FIELDS = " id, " + INSERT_FIELDS;

    @Insert({"insert into ", TABLE_NAME, "(", INSERT_FIELDS,
            ") values (#{userId},#{data},#{createdDate},#{type})"})
    int addFeed(Feed feed);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where id=#{id}"})
    Feed getFeedById(int id);



    @Select({
            "<script>",
            "select ", SELECT_FIELDS, " from ", TABLE_NAME,
            "    WHERE id &lt; #{maxId}",
            "        <if test='userIds.size() != 0'>" ,
            "    AND user_id in" ,
            "            <foreach item='item'  collection='userIds'" ,
            "    open='(' separator=',' close=')'>" ,
            "            #{item}" ,
            "            </foreach>" ,
            "        </if>" ,
            "    ORDER BY id DESC" ,
            "    LIMIT #{count}" ,
            "    </script>"})
    List<Feed> selectUserFeeds(@Param("maxId") int maxId,
                               @Param("userIds") List<Integer> userIds,
                               @Param("count") int count);

}